package com.custom.base.common.view

import android.content.Context
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.custom.base.R
import com.custom.util.setVisible
import kotlinx.android.synthetic.main.item_line_layout.view.*

class ItemLine @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :
    FrameLayout(context, attrs, defStyleAttr) {
    //    显示图标
    private var showLeftImage = true
    //    显示图标
    private var showRightImageBack = true
    //    显示文字
    private var showLeftText = true
    //显示圆点
    private var showRightImageRead = false
    //显示头像
    private var showRightImage = false

    private var leftTextContent: String? = null
    private var leftSrc: Drawable? = null
    private var rightSrc: Drawable? = null

    /**
     * 入口
     */
    init {
        //获取自定义属性
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.ItemLine)
        showLeftImage = typedArray.getBoolean(R.styleable.ItemLine_itemLineShowLeftImage, true)
        showLeftText = typedArray.getBoolean(R.styleable.ItemLine_itemLineShowLeftText, true)
        showRightImageRead = typedArray.getBoolean(R.styleable.ItemLine_itemLineShowRightImageRead, false)
        showRightImage = typedArray.getBoolean(R.styleable.ItemLine_itemLineShowRightImageBack, false)
        showRightImage = typedArray.getBoolean(R.styleable.ItemLine_itemLineShowRightImage, false)

        leftTextContent = typedArray.getString(R.styleable.ItemLine_itemLineLeftText)

        leftSrc = typedArray.getDrawable(R.styleable.ItemLine_itemLineLeftSrc)
        rightSrc = typedArray.getDrawable(R.styleable.ItemLine_itemLineRightSrc)
        //初始化视图
        initView()
        //        回收
        typedArray.recycle()
    }

    public lateinit var leftImage: ImageView
    public lateinit var leftText: TextView
    public lateinit var rightImage: ImageView
    public lateinit var rightImageRead: View
    public lateinit var rightImageBack: ImageView
    public lateinit var layout: LinearLayout

    /**
     * 初始化视图
     */
    private fun initView() {
        View.inflate(context, R.layout.item_line_layout, this)

        //控件赋值
        leftImage = left_image
        leftText = left_text
        rightImage = right_image
        rightImageRead = right_image_read
        rightImageBack = right_image_one
        layout = find_layout

        //设置展示情况
        left_image.setVisible(showLeftImage)
        left_text.setVisible(showLeftText)
        right_image_read.setVisible(showRightImageRead)
        right_image.setVisible(showRightImage)
        right_image_one.setVisible(showRightImage)

        //设置资源
        leftSrc?.let {
            left_image.setImageDrawable(it)
        }
        rightSrc?.let {
            right_image.setImageDrawable(it)
        }
        leftTextContent?.let {
            left_text.text = it
        }

    }

}
